<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.global.js"></script>
    <script src="../axios.min.js"></script>
</head>

<body>
    <div id="app">
        <p>
            Ask a yes/no question:
            <input v-model="question" />
        </p>
        <p>{{ answer }}</p>
    </div>

    <script>
        const app = {
            data() {
                return {
                    question: '',
                    answer: '臣妾不知道哇'
                }
            },
            watch: {
                // 每当 question 发生变化时，该函数将会执行
                question(newQuestion, oldQuestion) {
                    console.log("question changed", newQuestion, oldQuestion);
                    if (newQuestion.indexOf('?') > -1) {
                        this.getAnswer()
                    }
                }
            },
            methods: {
                getAnswer() {
                    this.answer = '让我想想...'
                    axios.get('https://yesno.wtf/api')
                        .then(response => {
                            this.answer = response.data.answer
                        })
                        .catch(error => {
                            this.answer = 'Error! Could not reach the API. ' + error
                        })
                }
            }
        }
        Vue.createApp(app).mount("#app")
    </script>
</body>

</html>